package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.controller.DrugDepositoryController;
import com.yixin.hospital.entity.bo.PageInfo;
import com.yixin.hospital.entity.domain.DrugDepositoryDrug;
import com.yixin.hospital.entity.domain.GodownCancelDrug;
import com.yixin.hospital.entity.dto.req.purchaseorder.GoDownCancelDrugReqDTO;
import com.yixin.hospital.entity.dto.req.purchaseorder.GoDownExchangeDrugStatusReqDTO;
import com.yixin.hospital.entity.vo.DrugDepositoryVo;
import com.yixin.hospital.entity.vo.GoDownCancelDrugVo;
import com.yixin.hospital.enums.DrugDepositoryOtpTypeEnum;
import com.yixin.hospital.enums.GoDownCancelStatusEnum;
import com.yixin.hospital.mapper.GodownCancelDrugMapper;
import com.yixin.hospital.mapper.extend.GodownCancelDrugExtendMapper;
import com.yixin.hospital.service.DrugDepositoryDrugService;
import com.yixin.hospital.service.GodownCancelDrugService;
import com.yixin.hospital.service.GodownDrugService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.yixin.hospital.common.Constant.format;

/**
* @author Administrator
* @description 针对表【godown_drug(返库药品表)】的数据库操作Service实现
* @createDate 2024-09-01 16:58:41
*/
@Service
public class GodownCancelDrugServiceImpl extends ServiceImpl<GodownCancelDrugMapper, GodownCancelDrug>
    implements GodownCancelDrugService {

    @Resource
    private GodownDrugService godownDrugService;
    @Resource
    private DrugDepositoryDrugService drugDepositoryDrugService;
    @Resource
    private DrugDepositoryController drugDepositoryController;
    @Resource
    private GodownCancelDrugExtendMapper godownCancelDrugExtendMapper;

    @Override
    public ApiResult<?> allList(HttpServletRequest request, int currentPage, int pageSize) {
        ApiResult<List<DrugDepositoryVo>> outputListApiResult = drugDepositoryController.availableList(request, DrugDepositoryOtpTypeEnum.OUTPUT.getCode());
        if (CollectionUtils.isEmpty(outputListApiResult.getData())) return ApiResult.success(outputListApiResult.getData());

        List<Long> drugDepositoryIds = outputListApiResult.getData().stream().map(DrugDepositoryVo::getId).collect(Collectors.toList());
        IPage<GoDownCancelDrugVo> page = godownCancelDrugExtendMapper.pageVo(Page.of(currentPage, pageSize), drugDepositoryIds);
        PageInfo<GoDownCancelDrugVo> pageInfo = new PageInfo<>();
        BeanUtil.copyProperties(page, pageInfo);
        pageInfo.setRecords(BeanUtil.copyToList(page.getRecords(), GoDownCancelDrugVo.class));
        return ApiResult.success(pageInfo);
    }

    @Override
    public ApiResult<?> listInfo(String code) {
        List<GoDownCancelDrugVo> detailList = godownCancelDrugExtendMapper.detail(code);
        return ApiResult.success(detailList);
    }

    @Override
    public ApiResult<?> goDownCancelDrug(GoDownCancelDrugReqDTO goDownDrugReqDTO, String userName) {
        List<GodownCancelDrug> godownDrugList = new ArrayList<>();
        String code = "FK" + LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
        for (GoDownCancelDrugReqDTO.GoDownDrugBO goDownDrugBO : goDownDrugReqDTO.getGoDownDrugBOList()) {
            GodownCancelDrug godownDrug = new GodownCancelDrug();
            godownDrug.setCode(code);
            godownDrug.setDrugDepositoryDrugId(goDownDrugBO.getDrugDepositoryDrugId());
            godownDrug.setSourceNum(goDownDrugBO.getSourceNum());
            godownDrug.setPutNum(goDownDrugBO.getNum());
            godownDrug.setCreatedBy(userName);
            godownDrug.setUpdatedBy(userName);
            godownDrugList.add(godownDrug);
        }
        this.saveBatch(godownDrugList);
        return ApiResult.success();
    }

    @Override
    public ApiResult<?> goDownCancelDrugStatus(GoDownExchangeDrugStatusReqDTO goDownExchangeDrugStatusReqDTO, String userName) {
        List<GodownCancelDrug> godownCancelDrugList = this.list(new LambdaQueryWrapper<GodownCancelDrug>().eq(GodownCancelDrug::getCode, goDownExchangeDrugStatusReqDTO.getCode()));
        GodownCancelDrug queryGodownCancelDrug = godownCancelDrugList.get(0);
        if (queryGodownCancelDrug.getStatus().equals(GoDownCancelStatusEnum.DOWN.getCode())) return ApiResult.error();
        List<Long> outputIds = godownCancelDrugList.stream().map(GodownCancelDrug::getDrugDepositoryDrugId).collect(Collectors.toList());
        Map<Long, DrugDepositoryDrug> drugDepositoryDrugMap = drugDepositoryDrugService.listByIds(outputIds).stream().collect(Collectors.toMap(DrugDepositoryDrug::getId, Function.identity()));
        List<DrugDepositoryDrug> drugDepositoryDrugList = new ArrayList<>();
        for (GodownCancelDrug godownCancelDrug : godownCancelDrugList) {
            // 减库存/加库存
            DrugDepositoryDrug drugDepositoryDrug;
            drugDepositoryDrug = drugDepositoryDrugMap.get(godownCancelDrug.getDrugDepositoryDrugId());
            drugDepositoryDrug.setNum(drugDepositoryDrug.getNum() - godownCancelDrug.getPutNum());
            // 改状态
            godownCancelDrug.setStatus(GoDownCancelStatusEnum.DOWN.getCode());
            godownCancelDrug.setUpdatedBy(userName);
            godownCancelDrug.setUpdatedTime(new Date());
            drugDepositoryDrug.setUpdatedBy(userName);
            drugDepositoryDrug.setUpdatedTime(new Date());
            drugDepositoryDrugList.add(drugDepositoryDrug);
        }
        this.updateBatchById(godownCancelDrugList);
        drugDepositoryDrugService.updateBatchById(drugDepositoryDrugList);
        return ApiResult.success();
    }
}




